package com.school.appleforgetbackend.service;

import com.alibaba.fastjson.JSON;
import com.school.appleforgetbackend.BO.entity.*;
import com.school.appleforgetbackend.BO.entity.huibujingqun.SbqkDataEntity;
import com.school.appleforgetbackend.BO.entity.huibujingqun.SbqkEntity;
import com.school.appleforgetbackend.BO.entity.huibujingqun.SbqkList1;
import com.school.appleforgetbackend.BO.entity.huibujingqun.SbqkList2;
import com.school.appleforgetbackend.BO.jsonObject.*;
import com.school.appleforgetbackend.BO.request.HomeRequest;
import com.school.appleforgetbackend.BO.request.SwxxRequest;
import com.school.appleforgetbackend.BO.response.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@ConditionalOnProperty(prefix = "mock.service", name = "user", havingValue = "false")
public class RealUnderService implements UnderService {

    // 优化后的代码
    @Override
    public SwxxResponse getswxx(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: "+ request);
            String pgName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                pgName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                pgName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                pgName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                pgName = "常村试验场";
            }
            else {
                pgName = "";
            }
            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/levelList?pgName="+pgName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            SwxxResponse swxxResponse = JSON.parseObject(originalResult, SwxxResponse.class);
            return swxxResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public EquipmentResponse getssqk(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);
            String areaName;
            // 获取name参数
            String name = request.getName();
            System.out.println("Processing with name parameter: " + name);
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/overview?pgName=" + areaName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            ThirdEquipment equipmentJson = JSON.parseObject(originalResult, ThirdEquipment.class);
            // 创建equipmentResponse对象
            EquipmentResponse equipmentResponse = new EquipmentResponse();

            if (equipmentJson != null && equipmentJson.getData() != null) {
                List<WellCount> wellCount = equipmentJson.getData().getWellCount();
                List<RechargeWellStatus> rechargeWellStatus = equipmentJson.getData().getRechargeWellStatus();
                List<WellEntity> data = new ArrayList<>();

                // 转换数据格式
                for (WellCount entity : wellCount) {
                    WellEntity wellEntity = new WellEntity();
                    wellEntity.setFcbCount(entity.getGradedScaleCount());
                    wellEntity.setHbjCount(entity.getRechargeWellCount());
                    wellEntity.setJqjCount(entity.getMonitorWellCount());
                    wellEntity.setKxsyljCount(entity.getPorePressureWellCount());
                    wellEntity.setAreaName(entity.getPgName());
                    data.add(wellEntity);
                }

                //设备状态 - 初始化状态计数
                int hbzCount = 0; // 回补中计数
                int kxzCount = 0; // 空闲计数
                int hyzCount = 0; // 空闲计数
                
                for (RechargeWellStatus entity : rechargeWellStatus) {
                    String status = entity.getStatus();
                    int count = Integer.parseInt(entity.getCount());
                    
                    // 根据状态值进行统计
                    if ("回补中".equals(status)) {
                        hbzCount = count;
                    } else if ("空闲".equals(status)) {
                        kxzCount = count;
                    }else if ("回扬中".equals(status)) {
                        hyzCount = count;
                    }
                    
                    // 仍然添加到data列表以便后续处理
                    WellEntity wellEntity = new WellEntity();
                    wellEntity.setHbz(String.valueOf(count));
                    wellEntity.setKxz(String.valueOf(count));
                    wellEntity.setHyz(String.valueOf(count));
                    data.add(wellEntity);
                }



                String msg = equipmentJson.getMsg();
                String code = equipmentJson.getCode();
                System.out.println("获取到的wellCount列表: " + wellCount);

                // 根据areaName进行筛选
                List<WellEntity> filteredData = data;
                if (areaName != null && !areaName.isEmpty()) {
                    filteredData = data.stream()
                            .filter(entity -> entity.getAreaName() != null && entity.getAreaName().equals(areaName))
                            .collect(Collectors.toList());
                    System.out.println("根据areaName筛选后的数据: " + filteredData);
                }

                // 创建EquipmentEntity并设置数据
                EquipmentEntity equipmentEntity = new EquipmentEntity();
                // 直接设置数据列表
                List<WellEntity> aas = new ArrayList<>();
                for (WellEntity Entity: filteredData) {
                    WellEntity wellCount1 =  new WellEntity();
                    wellCount1.setFcbCount(Entity.getFcbCount());
                    wellCount1.setKxsyljCount(Entity.getKxsyljCount());
                    wellCount1.setJqjCount(Entity.getJqjCount());
                    wellCount1.setHbjCount(Entity.getHbjCount());
                    wellCount1.setAreaName(Entity.getAreaName());
                    aas.add(wellCount1);
                }

                // 设置基本信息
                if (!filteredData.isEmpty()) {
                    equipmentEntity.setAreaName(filteredData.get(0).getAreaName());
                    equipmentEntity.setKxsyljCount(filteredData.get(0).getKxsyljCount());
                    equipmentEntity.setJqjCount(filteredData.get(0).getJqjCount());
                    equipmentEntity.setHbjCount(filteredData.get(0).getHbjCount());
                    equipmentEntity.setFcbCount(filteredData.get(0).getFcbCount());
                }
                
                // 设置从rechargeWellStatus中获取的状态值
                equipmentEntity.setHbz(String.valueOf(hbzCount)); // 回补中计数
                equipmentEntity.setKxz(String.valueOf(kxzCount)); // 空闲计数
                equipmentEntity.setHyz(String.valueOf(hyzCount)); // 空闲计数

                // 设置响应信息
                equipmentResponse.setData(equipmentEntity);
                equipmentResponse.setMsg(msg);
                equipmentResponse.setCode(code);
                System.out.println("成功将筛选后的数据设置到EquipmentResponse");
            }
            return equipmentResponse;
        } catch (Exception e) {
            System.out.println("Error in getssqk method: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public WaterQualityResponse getQuality(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9003/api//aqr/statistic/syc/qualityData";
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");

            String jsonString = JSON.toJSONString(request);
            HttpEntity<String> requestHttpEntity = new HttpEntity<>(jsonString, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            WaterQualityResponse waterQualityResponse = JSON.parseObject(originalResult, WaterQualityResponse.class);
//            for (SwxxDataEntity entity : swxxResponse.getData()) {
//                entity.setId(1L);
//            }
            return waterQualityResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TrafficResponse getFlowData(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9003/api//aqr/statistic/syc/flowData";
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");

            String jsonString = JSON.toJSONString(request);
            HttpEntity<String> requestHttpEntity = new HttpEntity<>(jsonString, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            TrafficResponse trafficResponse = JSON.parseObject(originalResult, TrafficResponse.class);
//            for (SwxxDataEntity entity : swxxResponse.getData()) {
//                entity.setId(1L);
//            }
            return trafficResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ReWellResponse gethbj(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: "+ request );
            String name = request.getName();
            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = "";
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/rechargeWellGroup?pgName=";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdSbEquipmentResponse thirdSbEquipmentResponse = JSON.parseObject(originalResult, ThirdSbEquipmentResponse.class);
            ReWellResponse reWellResponse = new ReWellResponse();
            List<DeviceCount> rechargeWellCount = thirdSbEquipmentResponse.getData().getRechargeWellCount();
            List<ThirdRechargeWellInfo> rechargeWellInfo = thirdSbEquipmentResponse.getData().getRechargeWellInfo();
            String code = thirdSbEquipmentResponse.getCode();
            String msg = thirdSbEquipmentResponse.getMsg();
            List<ReWellEntity> data = new ArrayList<>();
            for (ThirdRechargeWellInfo info:rechargeWellInfo) {
                // 支持多个区域名称和空字符串
                if ("侯庄试验场".equals(areaName) || "乐亭区".equals(areaName) || "运河区".equals(areaName) || "常村试验场".equals(areaName) || areaName.isEmpty()) {
                    // 根据areaName筛选对应的pgName
                    if (("侯庄试验场".equals(areaName) && "侯庄试验场".equals(info.getPgName())) || 
                        ("乐亭区".equals(areaName) && "乐亭区".equals(info.getPgName())) || 
                        ("运河区".equals(areaName) && "运河区".equals(info.getPgName())) || 
                        ("常村试验场".equals(areaName) && "常村试验场".equals(info.getPgName())) ||
                        areaName.isEmpty()) {
                        // 添加name参数过滤条件
                        if (name == null || name.isEmpty() || (info.getName() != null && info.getName().equals(name))) {
                            ReWellEntity reWellEntity = new ReWellEntity();
                            reWellEntity.setName(info.getName());
                            reWellEntity.setElevation(info.getElevation());
                            reWellEntity.setRechargeLayer(info.getRechargeLayer());
                            reWellEntity.setCode(info.getCode());
                            reWellEntity.setDepth(info.getDepth());
                            reWellEntity.setPromptFlow(info.getPromptFlow());
                            reWellEntity.setWaterDepth(info.getWaterDepth());
                            reWellEntity.setWaterTemp(info.getWaterTemp());
                            reWellEntity.setWaterPressure(info.getWaterPressure());
                            reWellEntity.setWaterLevel(info.getWaterLevel());
                            reWellEntity.setPgName(info.getPgName());
                            reWellEntity.setCumulativeBackfillCount(info.getCumulativeBackfillCount());
                            reWellEntity.setCumulativeEchoCount(info.getCumulativeEchoCount());
                            reWellEntity.setStatus(info.getStatus());
                            data.add(reWellEntity);
                        }
                    }
                }
            }
            reWellResponse.setCode(code);
            reWellResponse.setMsg(msg);
            reWellResponse.setData(data);
            return reWellResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SbEquipmentResponse getsbqk(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);
            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/rechargeWellGroup?pgName="+areaName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdSbEquipmentResponse thirdSbEquipmentResponse = JSON.parseObject(originalResult, ThirdSbEquipmentResponse.class);
            SbEquipmentResponse sbEquipmentResponse = new SbEquipmentResponse();
            List<DeviceCount> rechargeWellCount = thirdSbEquipmentResponse.getData().getRechargeWellCount();
            List<ThirdRechargeWellInfo> rechargeWellInfo = thirdSbEquipmentResponse.getData().getRechargeWellInfo();
            String code = thirdSbEquipmentResponse.getCode();
            String msg = thirdSbEquipmentResponse.getMsg();
            List<SbEquipmentEntity> data = new ArrayList<>();
            for (DeviceCount count: rechargeWellCount) {
                // 当areaName为"侯庄试验场"时，只添加pgName也为"侯庄试验场"且facilityType为"回补井"的数据
                if ("侯庄试验场".equals(areaName)) {
                    if ("侯庄试验场".equals(count.getPgName()) && "回补井".equals(count.getFacilityType())) {
                        SbEquipmentEntity sbEquipmentEntity = new SbEquipmentEntity();
                        sbEquipmentEntity.setTotal(count.getTotal());
                        sbEquipmentEntity.setOnline(count.getOnline());
                        sbEquipmentEntity.setOfflinne(count.getOfflinne());
                        sbEquipmentEntity.setPgName(count.getPgName());
                        sbEquipmentEntity.setFacilityType(count.getFacilityType());
                        sbEquipmentEntity.setTypeName(count.getTypeName());
                        data.add(sbEquipmentEntity);
                    }
                } else {
                    // 其他情况下，添加所有数据
                    SbEquipmentEntity sbEquipmentEntity = new SbEquipmentEntity();
                    sbEquipmentEntity.setTotal(count.getTotal());
                    sbEquipmentEntity.setOnline(count.getOnline());
                    sbEquipmentEntity.setOfflinne(count.getOfflinne());
                    sbEquipmentEntity.setPgName(count.getPgName());
                    sbEquipmentEntity.setFacilityType(count.getFacilityType());
                    sbEquipmentEntity.setTypeName(count.getTypeName());
                    data.add(sbEquipmentEntity);
                }
            }

//            for (ThirdRechargeWellInfo info:rechargeWellInfo) {
//                SbEquipmentEntity sbEquipmentEntity = new SbEquipmentEntity();
//                sbEquipmentEntity.setName(info.getName());
//                sbEquipmentEntity.setElevation(info.getElevation());
//                sbEquipmentEntity.setRechargeLayer(info.getRechargeLayer());
//                sbEquipmentEntity.setCode(info.getCode());
//                sbEquipmentEntity.setDepth(info.getDepth());
//                sbEquipmentEntity.setPromptFlow(info.getPromptFlow());
//                sbEquipmentEntity.setWaterDepth(info.getWaterDepth());
//                sbEquipmentEntity.setWaterTemp(info.getWaterTemp());
//                sbEquipmentEntity.setWaterPressure(info.getWaterPressure());
//                sbEquipmentEntity.setWaterLevel(info.getWaterLevel());
//                data.add(sbEquipmentEntity);
//            }
            // 调试日志 - 检查最终data列表
            System.out.println("Final data list size: " + data.size());
            System.out.println("Data list content: " + data);
            
            sbEquipmentResponse.setCode(code);
            sbEquipmentResponse.setMsg(msg);
            sbEquipmentResponse.setData(data);
            
            // 调试日志 - 检查最终响应对象
            System.out.println("Final SbEquipmentResponse data size: " + 
                              (sbEquipmentResponse.getData() != null ? sbEquipmentResponse.getData().size() : "null"));
            
            return sbEquipmentResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public AnalyzeResponse getbsfx(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9003/api//aqr/statistic/hbjq/bsfx";
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");

            String jsonString = JSON.toJSONString(request);
            HttpEntity<String> requestHttpEntity = new HttpEntity<>(jsonString, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            AnalyzeResponse analyzeResponse = JSON.parseObject(originalResult, AnalyzeResponse.class);
//            for (SwxxDataEntity entity : swxxResponse.getData()) {
//                entity.setId(1L);
//            }
            return analyzeResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ReWellingResponse gethuibu(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " );

            ReWellingResponse reWellingResponse = new ReWellingResponse();
            //设备情况
            SbEquipmentResponse getsbqk = getsbqk(request);
            List<SbEquipmentEntity> dataSb = getsbqk.getData();
            List<SbqkList1> list1 = new ArrayList<>();
            List<SbqkList2> list2 = new ArrayList<>();
            // 创建两个独立的data集合，分别用于不同类型的设备
            List<SbqkDataEntity> data1 = new ArrayList<>(); // 用于水位计和电磁流量计
            List<SbqkDataEntity> data2 = new ArrayList<>(); // 用于电动球阀和潜水泵
            // 设置pgName
            String pgName = dataSb != null && !dataSb.isEmpty() ? dataSb.get(0).getPgName() : "设备情况";
            for (SbEquipmentEntity entity:dataSb) {
                SbqkDataEntity sbqkDataEntity = new SbqkDataEntity();
                sbqkDataEntity.setName(entity.getPgName());
                sbqkDataEntity.setName(entity.getTypeName());
                sbqkDataEntity.setZongshu(entity.getTotal());
                sbqkDataEntity.setZaixian(entity.getOnline());
                sbqkDataEntity.setLixian(entity.getOfflinne());
                // 根据typeName的值将数据放入不同的集合
                String typeName = entity.getTypeName();
                if ("水位计".equals(typeName) || "电磁流量计".equals(typeName)) {
                    // 水位计和电磁流量计放入data1
                    data1.add(sbqkDataEntity);
                } else if ("电动球阀".equals(typeName) || "压力表".equals(typeName)) {
                    // 电动球阀和潜水泵放入data2
                    data2.add(sbqkDataEntity);
                }
            }
            
            // 将data1集合的值放到list1集合中
            if (!data1.isEmpty()) {
                SbqkList1 sbqkList1 = new SbqkList1();
                sbqkList1.setName(pgName);
                sbqkList1.setData(data1);
                list1.add(sbqkList1);
            }

            // 将data2集合的值放到list2集合中
            if (!data2.isEmpty()) {
                SbqkList2 sbqkList2 = new SbqkList2();
                sbqkList2.setName(pgName); // 设置名称，与list1保持一致
                sbqkList2.setData(data2);
                list2.add(sbqkList2);
            }

            SbqkEntity sbqkEntity = new SbqkEntity();
            sbqkEntity.setList1(list1);  // 设置list1属性
            sbqkEntity.setList2(list2);  // 设置list2属性
            ;//  添加到sbqk集合中

            //实时监测
            List<ReWellEntity> jcdata = gethbj(request).getData();
            List<SsjcEntity> ssjc = new ArrayList<>();
            List<SsjcdataEntity> data = new ArrayList<>();
            for (ReWellEntity entity: jcdata) {
                SsjcdataEntity ssjcdataEntity = new SsjcdataEntity();
                ssjcdataEntity.setName(entity.getName());
                ssjcdataEntity.setCode(entity.getCode());
                ssjcdataEntity.setDepth(entity.getDepth());
                ssjcdataEntity.setElevation(entity.getElevation());
                ssjcdataEntity.setPgName(entity.getPgName());
                ssjcdataEntity.setWaterDepth(entity.getWaterDepth());
                ssjcdataEntity.setWaterLevel(entity.getWaterLevel());
                ssjcdataEntity.setWaterTemp(entity.getWaterTemp());
                ssjcdataEntity.setPromptFlow(entity.getPromptFlow());
                ssjcdataEntity.setRechargeLayer(entity.getRechargeLayer());
                ssjcdataEntity.setWaterPressure(entity.getWaterPressure());
                data.add(ssjcdataEntity);
            }


            SsjcEntity ssjcEntity = new SsjcEntity();
            ssjcEntity.setData(data);
            ssjcEntity.setName(pgName);
            ssjc.add(ssjcEntity);

            HuiBuJingqinRightEntity huibujingqinRight = new HuiBuJingqinRightEntity();
            huibujingqinRight.setSbqk(sbqkEntity);
            huibujingqinRight.setSsjc(ssjc);

            reWellingResponse.setHuibujingqinRight(huibujingqinRight);
            return reWellingResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public JcResponse getjiance(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " );

            JcResponse jcResponse = new JcResponse();
            List<JcEquipmentEntity> data = getjcjqsbqk(request).getData();
            List<JcWellEntity> data1 = getjcjqlist(request).getData();
            JiancejingqunEntity jiancejingqunDetail = new JiancejingqunEntity();
            JiancejingqunRightEntity jiancejingqunRight = new JiancejingqunRightEntity();
            List<JcSitesEntity> sites = new ArrayList<>();
            JcsbqkEntity sbqk = new JcsbqkEntity();
            List<JcssjcEntity> ssjc = new ArrayList<>();
            JcssjcEntity jcssjcEntity = new JcssjcEntity();
            List<JcssjcDataEntity> jcdata = new ArrayList<>();
            List<Listjc> list = new ArrayList<>();
            List<JcsbqkDataEntity> sbdata = new ArrayList<>();
            String pgName = data != null && !data.isEmpty() ? data.get(0).getPgName() : "设备情况";
            //设备情况
            for (JcEquipmentEntity entity:data) {
                JcsbqkDataEntity jcsbqkDataEntity = new JcsbqkDataEntity();
                jcsbqkDataEntity.setName(entity.getPgName());
                jcsbqkDataEntity.setName(entity.getTypeName());
                jcsbqkDataEntity.setZongshu(entity.getTotal());
                jcsbqkDataEntity.setLixian(entity.getOfflinne());
                jcsbqkDataEntity.setZaixian(entity.getOnline());
                // 根据typeName的值将数据放入不同的集合
                String typeName = entity.getTypeName();
                if ("水位计".equals(typeName) || "水质传感器".equals(typeName) || "潜水泵".equals(typeName) || "静力水准仪".equals(typeName)) {
                    // 水位计和电磁流量计放入data1
                    sbdata.add(jcsbqkDataEntity);
                }
            }
            if (!data1.isEmpty()) {
                Listjc listjc = new Listjc();
                listjc.setName(pgName);
                listjc.setData(sbdata);
                list.add(listjc);
            }
            sbqk.setList(list);

            //实时监测
            for (JcWellEntity entity: data1) {
                JcssjcDataEntity jcssjcDataEntity = new JcssjcDataEntity();
                jcssjcDataEntity.setCode(entity.getCode());
                jcssjcDataEntity.setName(entity.getName());
                jcssjcDataEntity.setElevation(entity.getElevation());
                jcssjcDataEntity.setDepth(entity.getDepth());
//                jcssjcDataEntity.setEh(entity.getEh());
                jcssjcDataEntity.setDissolvedOxygen(entity.getDissolvedOxygen());
                jcssjcDataEntity.setWaterPh(entity.getWaterPh());
                jcssjcDataEntity.setReductionPotential(entity.getReductionPotential());
                jcssjcDataEntity.setTurbidity(entity.getTurbidity());
                jcssjcDataEntity.setPgName(entity.getPgName());
                jcssjcDataEntity.setTds(entity.getTds());
                jcssjcDataEntity.setConductivity(entity.getConductivity());
                jcssjcDataEntity.setWaterDepth(entity.getWaterDepth());
                jcssjcDataEntity.setWaterLevel(entity.getWaterLevel());
                jcssjcDataEntity.setWaterTemp(entity.getWaterTemp());
                jcssjcDataEntity.setMonitorLayer(entity.getMonitorLayer());
//                jcssjcDataEntity.setSearchKey(entity.getSearchKey());

                jcdata.add(jcssjcDataEntity);
            }
            jcssjcEntity.setName(pgName);
            jcssjcEntity.setData(jcdata);
            ssjc.add(jcssjcEntity);



            jiancejingqunRight.setSbqk(sbqk);
            jiancejingqunRight.setSsjc(ssjc);
            jcResponse.setJiancejingqunRight(jiancejingqunRight);
            return jcResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public LayeringResponse getjcjqfcb(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);
            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/monitorWellGroup?pgName=" + areaName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdJcWellResponse thirdJcWellResponse = JSON.parseObject(originalResult, ThirdJcWellResponse.class);
            String code = thirdJcWellResponse.getCode();
            String msg = thirdJcWellResponse.getMsg();

            List<GradedScale> gradedScale = thirdJcWellResponse.getData().getGradedScale();
            LayeringResponse layeringResponse = new LayeringResponse();
            List<LayeringEntity> data = new ArrayList<>();
            for (GradedScale graded:gradedScale) {
                LayeringEntity layeringEntity = new LayeringEntity();
                layeringEntity.setFacilityCode(graded.getFacilityCode());
                layeringEntity.setCode(graded.getCode());
                layeringEntity.setName(graded.getName());
                layeringEntity.setAddressSettlement(graded.getAddressSettlement());
                layeringEntity.setFacilityName(graded.getFacilityName());
                layeringEntity.setPgName(graded.getPgName());


                data.add(layeringEntity);
            }

            layeringResponse.setMsg(msg);
            layeringResponse.setCode(code);
            layeringResponse.setData(data);
            return layeringResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JcWellEntityResponse getjcjqlist(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);
            String areaName;
            String name = request.getName();
            System.out.println("Processing with name parameter: " + name);
            //桃城区试验场
            if (Objects.equals(request.getPgCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getPgCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getPgCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getPgCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = "";
            }

            RestTemplate restTemplate = new RestTemplate();
            StringBuilder urlBuilder = new StringBuilder("http://123.182.146.254:9009/openApi/digitaltwin/monitorWellGroup");
            
            // 构建URL参数
            boolean hasParam = false;
            if (areaName != null) {
                urlBuilder.append("?pgName=").append(areaName);
                hasParam = true;
            }
            
            if (name != null && !name.isEmpty()) {
                if (hasParam) {
                    urlBuilder.append("&");
                } else {
                    urlBuilder.append("?");
                }
                urlBuilder.append("name=").append(name);
            }
            
            String url = urlBuilder.toString();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdJcWellResponse thirdJcWellResponse = JSON.parseObject(originalResult, ThirdJcWellResponse.class);
            String code = thirdJcWellResponse.getCode();
            String msg = thirdJcWellResponse.getMsg();
            List<MonitorWellInfo> monitorWellInfo = thirdJcWellResponse.getData().getMonitorWellInfo();
            JcWellEntityResponse jcWellEntityResponse = new JcWellEntityResponse();
            List<JcWellEntity> data = new ArrayList<>();
            for (MonitorWellInfo info:monitorWellInfo) {
                // 添加调试日志
                System.out.println("Checking info: " + info.getName() + ", areaName: " + areaName + ", name param: " + name);
                
                // 修改过滤逻辑，确保正确处理name参数
                // 当areaName为null或空时也匹配成功，否则需要等于"侯庄试验场"
                boolean areaMatch = (areaName == null || areaName.isEmpty()) || "侯庄试验场".equals(areaName)|| "常村试验场".equals(areaName)
                        || "乐亭区".equals(areaName)|| "运河区".equals(areaName);
                boolean nameMatch = (name == null || name.isEmpty()) || 
                                   (info.getName() != null && info.getName().equals(name));
                
                System.out.println("Area match: " + areaMatch + ", Name match: " + nameMatch);
                
                if (areaMatch && nameMatch) {
                    JcWellEntity jcWellEntity = new JcWellEntity();
                    jcWellEntity.setElevation(info.getElevation());
                    jcWellEntity.setTds(info.getTds());
                    jcWellEntity.setCode(info.getCode());
                    jcWellEntity.setWaterDepth(info.getWaterDepth());
                    jcWellEntity.setReductionPotential(info.getOrp());
                    jcWellEntity.setPgName(info.getPgName());
                    jcWellEntity.setTurbidity(info.getNtu());
                    jcWellEntity.setDepth(info.getDepth());
                    jcWellEntity.setConductivity(info.getWec());
                    jcWellEntity.setWaterTemp(info.getWaterTemp());
                    jcWellEntity.setName(info.getName());
                    jcWellEntity.setWaterPh(info.getPh());
                    jcWellEntity.setMonitorLayer(info.getMonitorLayer());
                    jcWellEntity.setDissolvedOxygen(info.getDoc());
                    jcWellEntity.setWaterLevel(info.getWaterLevel());
                    data.add(jcWellEntity);
                }
            }

            jcWellEntityResponse.setMsg(msg);
            jcWellEntityResponse.setCode(code);
            jcWellEntityResponse.setData(data);
            return jcWellEntityResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JcEquipmentResponse getjcjqsbqk(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/monitorWellGroup?pgName=" + areaName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdJcWellResponse thirdJcWellResponse = JSON.parseObject(originalResult, ThirdJcWellResponse.class);
            String code = thirdJcWellResponse.getCode();
            String msg = thirdJcWellResponse.getMsg();
            List<MonitorWellCount> monitorWellCount = thirdJcWellResponse.getData().getMonitorWellCount();
            JcEquipmentResponse jcEquipmentResponse = new JcEquipmentResponse();
            List<JcEquipmentEntity> data = new ArrayList<>();
            for (MonitorWellCount count:monitorWellCount) {
                if ("侯庄试验场".equals(areaName)) {
                    if ("侯庄试验场".equals(count.getPgName()) && "监测井".equals(count.getFacilityType())) {
                        JcEquipmentEntity jcEquipmentEntity = new JcEquipmentEntity();
                        jcEquipmentEntity.setFacilityType(count.getFacilityType());
                        jcEquipmentEntity.setTypeName(count.getTypeName());
                        jcEquipmentEntity.setTotal(count.getTotal());
                        jcEquipmentEntity.setOfflinne(count.getOfflinne());
                        jcEquipmentEntity.setOnline(count.getOnline());
                        jcEquipmentEntity.setPgName(count.getPgName());
                        data.add(jcEquipmentEntity);
                    }
                }
                else if ("常村试验场".equals(areaName)){
                    if ("常村试验场".equals(count.getPgName()) && "监测井".equals(count.getFacilityType())) {
                        JcEquipmentEntity jcEquipmentEntity = new JcEquipmentEntity();
                        jcEquipmentEntity.setFacilityType(count.getFacilityType());
                        jcEquipmentEntity.setTypeName(count.getTypeName());
                        jcEquipmentEntity.setTotal(count.getTotal());
                        jcEquipmentEntity.setOfflinne(count.getOfflinne());
                        jcEquipmentEntity.setOnline(count.getOnline());
                        jcEquipmentEntity.setPgName(count.getPgName());
                        data.add(jcEquipmentEntity);
                    }
                }

                else {
                    JcEquipmentEntity jcEquipmentEntity = new JcEquipmentEntity();
                    jcEquipmentEntity.setFacilityType(count.getFacilityType());
                    jcEquipmentEntity.setTypeName(count.getTypeName());
                    jcEquipmentEntity.setTotal(count.getTotal());
                    jcEquipmentEntity.setOfflinne(count.getOfflinne());
                    jcEquipmentEntity.setOnline(count.getOnline());
                    jcEquipmentEntity.setPgName(count.getPgName());
                    data.add(jcEquipmentEntity);
                }
            }

            jcEquipmentResponse.setMsg(msg);
            jcEquipmentResponse.setCode(code);
            jcEquipmentResponse.setData(data);

            return jcEquipmentResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SwxxResponse getHomeswxx(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: "+ request);
            String pgName;
            //桃城区试验场
            if (Objects.equals(request.getCode(), "HSTCQHZSYC")) {
                pgName = "侯庄试验场";
            }
            else if (Objects.equals(request.getCode(), "HSLTXHZSYC")){
                pgName = "乐亭区";
            }
            else if (Objects.equals(request.getCode(), "HSYHQHZSYC")){
                pgName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getCode(), "HSFCXCCSYC")){
                pgName = "常村试验场";
            }
            else {
                pgName = "";
            }
            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/levelList?pgName="+pgName;
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            SwxxResponse swxxResponse = JSON.parseObject(originalResult, SwxxResponse.class);
            return swxxResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CumulativeResponse getHomeljsc(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request );

            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getPgCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getPgCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getPgCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getPgCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }
            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/home";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdCumulativeResponse thirdCumulativeResponse = JSON.parseObject(originalResult, ThirdCumulativeResponse.class);
//            String cumulativeEchoCount = thirdCumulativeResponse.getData().getCountAndEchoCount().getCumulativeEchoCount();
            String code = thirdCumulativeResponse.getCode();
            String msg = thirdCumulativeResponse.getMsg();
            AreaEntity areaEntity = new AreaEntity();
            CumulativeResponse cumulativeResponse = new CumulativeResponse();
            List<PercentByGroup> percentByGroup = thirdCumulativeResponse.getData().getPercentByGroup();
            if (percentByGroup != null && !percentByGroup.isEmpty()) {
                // 根据areaName查找对应的PercentByGroup对象
                for (PercentByGroup percent : percentByGroup) {
                    // 检查是否匹配侯庄试验场或常村试验场
                    if (("侯庄试验场".equals(areaName) && "侯庄试验场".equals(percent.getName())) ||
                        ("常村试验场".equals(areaName) && "常村试验场".equals(percent.getName()))) {
                        // 设置对应的数据
                        areaEntity.setBackfillCount(percent.getCumulativeBackfillCount());
                        areaEntity.setAreaName(percent.getName());
                        break; // 找到匹配项后退出循环
                    }
                }
                
                // 如果没有找到匹配的，使用第一个数据作为默认值
                if (areaEntity.getBackfillCount() == null) {
                    PercentByGroup defaultPercent = percentByGroup.get(0);
                    areaEntity.setBackfillCount(defaultPercent.getCumulativeBackfillCount());
                    areaEntity.setAreaName(defaultPercent.getName());
                }
            }
//            areaEntity.setBackfillCount(cumulativeEchoCount);
            cumulativeResponse.setData(areaEntity);
            cumulativeResponse.setMsg(msg);
            cumulativeResponse.setCode(code);
            return cumulativeResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public InformationResponse getGroundList(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request );

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/home";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdCumulativeResponse thirdCumulativeResponse = JSON.parseObject(originalResult, ThirdCumulativeResponse.class);
            String cumulativeEchoCount = thirdCumulativeResponse.getData().getCountAndEchoCount().getCumulativeBackfillCount();
            String code = thirdCumulativeResponse.getCode();
            String msg = thirdCumulativeResponse.getMsg();
            InformationData informationData = new InformationData();
            InformationResponse informationResponse = new InformationResponse();
            List<InformationEntity> data = new ArrayList<>();

            List<PercentByGroup> percentByGroup = thirdCumulativeResponse.getData().getPercentByGroup();
            for (PercentByGroup percent: percentByGroup) {
                InformationEntity informationEntity = new InformationEntity();
                informationEntity.setName(percent.getName());
                if ("侯庄试验场".equals(percent.getName())) {
                     informationEntity.setAreaName("桃城区");
                 }
                else if ("常村试验场".equals(percent.getName())) {
                    informationEntity.setAreaName("阜城县");
                }
                else if ("运河区试验场".equals(percent.getName())) {
                    informationEntity.setAreaName("运河区");
                }
                else if ("乐亭县试验场".equals(percent.getName())) {
                    informationEntity.setAreaName("乐亭县");
                }

                informationEntity.setPercent(percent.getPercent());
                informationEntity.setCumulativeBackfillCount(percent.getCumulativeBackfillCount());
                data.add(informationEntity);
            }
            //累计回补量
            informationData.setCumulativeBackfillCount(cumulativeEchoCount);

            informationData.setList(data);
            informationResponse.setData(informationData);
            informationResponse.setMsg(msg);
            informationResponse.setCode(code);
            return informationResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CountAndEchoResponse getHomejqtj(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request );
            String areaName;
            //桃城区试验场
            if (Objects.equals(request.getPgCode(), "HSTCQHZSYC")) {
                areaName = "侯庄试验场";
            }
            else if (Objects.equals(request.getPgCode(), "HSLTXHZSYC")){
                areaName = "乐亭区";
            }
            else if (Objects.equals(request.getPgCode(), "HSYHQHZSYC")){
                areaName = "运河区";
            }
            //阜城县试验场
            else if (Objects.equals(request.getPgCode(), "HSFCXCCSYC")){
                areaName = "常村试验场";
            }
            else {
                areaName = null;
            }

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/home";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);
            ThirdWellCount wellCountJson = JSON.parseObject(originalResult, ThirdWellCount.class);
            // 创建CountAndEchoResponse对象
            CountAndEchoResponse countAndEchoResponse = new CountAndEchoResponse();

            if (wellCountJson != null && wellCountJson.getData() != null) {
                List<WellCount> wellCount = wellCountJson.getData().getWellCount();
                List<WellEntity> data = new ArrayList<>();
                for (WellCount entity:wellCount){
                    WellEntity wellEntity = new WellEntity();
                    wellEntity.setFcbCount(entity.getGradedScaleCount());
                    wellEntity.setHbjCount(entity.getRechargeWellCount());
                    wellEntity.setJqjCount(entity.getMonitorWellCount());
                    wellEntity.setKxsyljCount(entity.getPorePressureWellCount());
                    wellEntity.setAreaName(entity.getPgName());
                    data.add(wellEntity);
                }
                String msg = wellCountJson.getMsg();
                String code = wellCountJson.getCode();
                System.out.println("获取到的wellCount列表: " + wellCount);

                // 将wellCount列表设置到CountAndEchoResponse
                List<WellEntity> filteredData = data;
                if (areaName != null && !areaName.isEmpty()) {
                    filteredData = data.stream()
                            .filter(entity -> entity.getAreaName() != null && entity.getAreaName().equals(areaName))
                            .collect(Collectors.toList());
                    System.out.println("根据areaName筛选后的数据: " + filteredData);
                }

                // 将筛选后的数据设置到CountAndEchoResponse
                countAndEchoResponse.setData(filteredData);
                countAndEchoResponse.setMsg(msg);
                countAndEchoResponse.setCode(code);
                System.out.println("成功将wellCount列表设置到CountAndEchoResponse.data");
            }
            return countAndEchoResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public WellResponse getHomejqfb(HomeRequest request) {
        try {
        // 打印接收的参数，验证是否正确传递
        System.out.println("Received body parameter: " + request );

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://123.182.146.254:9009/openApi/digitaltwin/home";
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

        HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
        String originalResult = response.getBody();
        System.out.println("Original Response: " + originalResult);
        ThirdWellCount wellCountJson = JSON.parseObject(originalResult, ThirdWellCount.class);
        // 创建WellCountJson对象
        WellResponse wellResponse = new WellResponse();

        if (wellCountJson != null && wellCountJson.getData() != null) {
            List<WellCount> wellCount = wellCountJson.getData().getWellCount();
            List<WellEntity> data = new ArrayList<>();
            for (WellCount entity:wellCount){
                WellEntity wellEntity = new WellEntity();
                wellEntity.setFcbCount(entity.getGradedScaleCount());
                wellEntity.setHbjCount(entity.getRechargeWellCount());
                wellEntity.setJqjCount(entity.getMonitorWellCount());
                wellEntity.setKxsyljCount(entity.getPorePressureWellCount());
                if ("侯庄试验场".equals(entity.getPgName())) {
                    wellEntity.setAreaName("桃城区");
                }
                else if ("常村试验场".equals(entity.getPgName())) {
                    wellEntity.setAreaName("阜城县");
                }
                else if ("运河区".equals(entity.getPgName())) {
                    wellEntity.setAreaName("运河区");
                }
                else if ("乐亭区".equals(entity.getPgName())) {
                    wellEntity.setAreaName("乐亭县");
                }
//                wellEntity.setAreaName(entity.getPgName());
                data.add(wellEntity);
            }
            String code = wellCountJson.getCode();
            String msg = wellCountJson.getMsg();
            System.out.println("获取到的wellCount列表: " + wellCount);

            // 将wellCount列表设置到CountAndEchoResponse

            wellResponse.setData(data);
            wellResponse.setMsg(msg);
            wellResponse.setCode(code);
            System.out.println("成功将wellCount列表设置到CountAndEchoResponse.data");
        }
            return wellResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public ExperimentResponse getTestList(SwxxRequest request) {
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi/digitaltwin/listTestProject?pgName=";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdHistoryExperimentResponse thirdExperimentResponse = JSON.parseObject(originalResult, ThirdHistoryExperimentResponse.class);
            List<ThirdHistoryExperiment>thirdHistoryExperiment = thirdExperimentResponse.getData();
            String code = thirdExperimentResponse.getCode();
            String msg = thirdExperimentResponse.getMsg();
            ExperimentResponse experimentResponse = new ExperimentResponse();
            List<ExperimentEntity> list = new ArrayList<>();
            TestProjectDateEntity data = new TestProjectDateEntity();
            for (ThirdHistoryExperiment experiment:thirdHistoryExperiment) {
                ExperimentEntity experimentEntity = new ExperimentEntity();
                experimentEntity.setId(experiment.getId());
                experimentEntity.setStartTime(experiment.getStartTime());
                experimentEntity.setEndTime(experiment.getEndTime());
                experimentEntity.setTempName(experiment.getName());
                experimentEntity.setStatus(experiment.getStatus());

                list.add(experimentEntity);
            }
            data.setList(list);
            data.setTotal("");

            experimentResponse.setMsg(msg);
            experimentResponse.setCode(code);
            experimentResponse.setData(data);

            return experimentResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BDExperimentResponse getTestResourceList(SwxxRequest request){
        try {
            // 打印接收的参数，验证是否正确传递
            System.out.println("Received body parameter: " + request);

            RestTemplate restTemplate = new RestTemplate();
            String url = "http://123.182.146.254:9009/openApi//digitaltwin/listResourceDevice?id=1";
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.set("client", "iOIWB3xWhVmgu91kKIag7LWGCMkhibaInyW3");

            HttpEntity<String> requestHttpEntity = new HttpEntity<>(null, headers);
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestHttpEntity, String.class);
            String originalResult = response.getBody();
            System.out.println("Original Response: " + originalResult);

            ThirdHistoryExperimentResponse thirdExperimentResponse = JSON.parseObject(originalResult, ThirdHistoryExperimentResponse.class);
            List<ThirdHistoryExperiment>thirdHistoryExperiment = thirdExperimentResponse.getData();
            String code = thirdExperimentResponse.getCode();
            String msg = thirdExperimentResponse.getMsg();
            BDExperimentResponse bDexperimentResponse = new BDExperimentResponse();
            List<ExperimentEntity> list = new ArrayList<>();
            TestProjectDateEntity data = new TestProjectDateEntity();
            for (ThirdHistoryExperiment experiment:thirdHistoryExperiment) {
                ExperimentEntity experimentEntity = new ExperimentEntity();
                experimentEntity.setId(experiment.getId());
                experimentEntity.setStartTime(experiment.getStartTime());
                experimentEntity.setEndTime(experiment.getEndTime());
                experimentEntity.setTempName(experiment.getName());
                experimentEntity.setStatus(experiment.getStatus());

                list.add(experimentEntity);
            }
            data.setList(list);
            data.setTotal("");

            bDexperimentResponse.setMsg(msg);
            bDexperimentResponse.setCode(code);
//            bDexperimentResponse.setData(data);

            return bDexperimentResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
